<?php


require_once('config.php');
require_once(BASE_URL.'includes/core.cleaning.functions.php');
if(DB_USE == 'mysql')
	require_once(BASE_URL.'includes/db/mysql.core.class.php');
elseif(DB_USE == 'sqlite')
	require_once(BASE_URL.'includes/db/sqlite.core.class.php');
else
	die('no db configuration found');
require_once(BASE_URL.'includes/core.functions.class.php');
require_once(BASE_URL.'includes/core.upload.theme.class.php');
require_once(BASE_URL.'includes/core.theme.manger.class.php');
require_once(BASE_URL.'includes/core.plugins.class.php');
require_once(BASE_URL.'includes/admin/core.admin.class.php');
//(BASE_URL.'includes/admin/permissions.php');
$admin = new admin;
$admin->getPluginList();
$theme = new themeManager;
$plugins = $admin->loadPlugins();
if(!is_object($page))
	$page = &$admin;
if(!is_object($admin))
	$admin = &$page;
foreach($plugins as $value) {
	eval('require("'.$value.'");');
}
$admin->getInterfacePlugins($pluginData);

if ($_GET['login'] == true) {
	if($_POST['login']) {
		$set = $admin->set_validation($_POST['username'], $_POST['password']);
		if($set == false) {
			$sys_content = $admin->message('error', "Whoops! Invalid Login!", "Please try entering the username and password again.");
		}
		else {
			if($_GET['redirect']) {
				if($_GET['redirect'] == 'lastpage')
					$redirectPage = $_SERVER["HTTP_REFERER"];
				else
					$redirectPage = $_GET['redirect'];
			}
			else {
				$redirectPage ='admin.php';
			}
			$admin->validate("", $redirectPage);
		}
	}
	include(BASE_URL.'includes/admin/pages/login.php');
	exit();
}
else {
	$reUrl = explode(THEME_URL, $_SERVER['REQUEST_URI']);
	$admin->validate(THEME_URL."admin.php?login=true&redirect=".$reUrl[1], "");
}
if($_GET['page'] && isset($_GET['page'])) {
	$admin->page_info['id'] = $_GET['page'];
}

elseif ($_GET['logout'] == "true") {
	$_SESSION = array();
	session_destroy();
	header('location: '.THEME_URL);
}
else {
	$admin->page_info['id'] = "dashboard";
}
$perUrl = BASE_URL.'includes/admin/permissions.php';
$admin->page_info['permissions'] = unserialize(file_get_contents($perUrl));
if($admin->canAccess(ucwords(str_replace('-', ' ',$admin->page_info['id']))))
	define("INCLUDE_URL", BASE_URL.'includes/admin/pages/'.$admin->page_info['id'].'.php');
else
	define("INCLUDE_URL", BASE_URL.'includes/admin/pages/no-access.php');
include(INCLUDE_URL);
?>